package com.chalk.common.baiduAPI;

import com.chalk.common.util.GsonUtil;
import com.chalk.common.util.HttpClientUtil;
import org.springframework.stereotype.Component;

@Component
public class BaiduServe {

    public static final String AK = "52oREFBPDOzxGKNKl0Rvm37hTus88mcL";


    /**
     * 根据经纬度获取当前城市名称
     *
     * @param lat 纬度
     * @param lng 经度
     * @return
     */
    public BaiduResult getCityName(Float lat, Float lng) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=")
                .append(lat).append(",").append(lng).append("&output=json&pois=0&latest_admin=1&ak=").append(AK);

        String s = HttpClientUtil.get(String.valueOf(stringBuilder));
        String str = s.substring(s.indexOf("(") + 1, s.length() - 1);
        BaiduResult baiduResult = new BaiduResult();
        BaiduResult baiduResultQuery = GsonUtil.jsonToBean(str, baiduResult.getClass());
        return baiduResultQuery;
    }

    /**
     * 根据当前城市名称获取经纬度
     *
     * @return
     */
    public BaiduResult getLatLng(String address) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("http://api.map.baidu.com/geocoder/v2/?address=")
                .append(address).append("&output=json&ak=").append(AK).append("&callback=showLocation");

        String s = HttpClientUtil.get(String.valueOf(stringBuilder).replace(" ", "%20"));
        String str = s.substring(s.indexOf("(") + 1, s.length() - 1);
        BaiduResult baiduResult = new BaiduResult();
        BaiduResult baiduResultQuery = GsonUtil.jsonToBean(str, baiduResult.getClass());
        return baiduResultQuery;
    }

}